package com.zkca.xf.presenter;

import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.zkca.xf.base.BasePresenter;
import com.zkca.xf.manager.DataManager;
import com.zkca.xf.model.javabean.DeviceType;
import com.zkca.xf.model.javabean.uploadbean.RequestInspect;
import com.zkca.xf.view.DeviceAddView;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DefaultObserver;
import io.reactivex.schedulers.Schedulers;


/**
 * Created by li on 2018/11/19.
 */

public class DeviceAddPresenter extends BasePresenter {

    private DataManager dataManager;

    private DeviceAddView xunDianAddView;

    public DeviceAddPresenter(LifecycleProvider<ActivityEvent> provider, DeviceAddView xunDianAddView) {
        super(provider);
        dataManager = DataManager.getInstance();
        this.xunDianAddView=xunDianAddView;
    }

    public void getDvType(RequestInspect requestPage) {
        xunDianAddView.showProgressDialog();
        dataManager.getDvType(requestPage)
                .subscribeOn(Schedulers.io())// 在子线程中进行Http访问
                .observeOn(AndroidSchedulers.mainThread())// UI线程处理返回接口
                .compose(getProvider().<DeviceType>bindUntilEvent(ActivityEvent.DESTROY))// onDestroy取消订阅
                .subscribe(new DefaultObserver<DeviceType>() {// 订阅
                    @Override
                    public void onNext(DeviceType data) {
                        xunDianAddView.getDvtype(data);
                        xunDianAddView.hideProgressDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        xunDianAddView.showError(e.getMessage());
                        xunDianAddView.hideProgressDialog();
                    }

                    @Override
                    public void onComplete() {


                    }
                });
    }


    public void addDevice(RequestInspect requestPage) {

        dataManager.addDevice(requestPage)
                .subscribeOn(Schedulers.io())// 在子线程中进行Http访问
                .observeOn(AndroidSchedulers.mainThread())// UI线程处理返回接口
                .compose(getProvider().<String>bindUntilEvent(ActivityEvent.DESTROY))// onDestroy取消订阅
                .subscribe(new DefaultObserver<String>() {// 订阅
                    @Override
                    public void onNext(String data) {
                        xunDianAddView.inPut(data);
                        xunDianAddView.hideProgressDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        xunDianAddView.showError(e.getMessage());
                        xunDianAddView.hideProgressDialog();
                    }

                    @Override
                    public void onComplete() {
                    }
                });
    }

}
